<?php

if(isset($_GET['area']) && $_GET['area'] > 0 && isset($_GET['radius'])){

require('settings.php');
require('dbConn.php');

// grab main area coords

$sql = "SELECT latitude, longitude FROM areas WHERE id = '".mysql_real_escape_string($_GET['area'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$rs1=mysql_fetch_assoc($query);

// grab all areas for dropdown

$sql = "SELECT id, area FROM areas ORDER BY area";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
while($rs2=mysql_fetch_assoc($query)){
$areasArr[$rs2['id']] = $rs2['area'];
}

// grab local areas

$sql = "SELECT id, area, ROUND(SQRT(POW((69.1 * (".$rs1['latitude']." - latitude)), 2) + POW((53 * (".$rs1['longitude']." - longitude)), 2)), 1) AS distance 
FROM areas HAVING distance <= ".mysql_real_escape_string($_GET['radius'])." AND id != '".mysql_real_escape_string($_GET['area'])."' ORDER BY distance LIMIT 0,9";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

$i = 1;

$dropdownHTML= '';

while($rs3=mysql_fetch_assoc($query)){

	$dropdownHTML.= '<div id="area_container_'.$i.'"><select name="area_'.$i.'" id="area_'.$i.'" class="textbox" onfocus="return highlight(\'areas\');" onblur="return removeHighlight(\'areas\');"><option value="">Please select</option>';

	foreach($areasArr as $key => $value){

	$selected = ($key == $rs3['id']) ? ' selected="selected"' : '';

	$dropdownHTML.= '<option value="'.$key.'"'.$selected.'>'.$value.'</option>';

	}

	$dropdownHTML.= '</select></div>';

$i++;

}

echo $i.'^_^_SPLIT_^_^'.$dropdownHTML;

}

?>